const url = require('url');

let obj = {
  protocol: 'https:',
  slashes: true,
  auth: 'user:pass',
  host: 'sub.host.com:8080',
  port: '8080',
  hostname: 'sub.host.com',
  hash: '#aa',
  search: '?query=string',
  query: 'query=string',
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?query=string',
  // href: 'https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash'
}
// 将对象转化成url
const myURL1 = url.format(obj)
console.log(myURL1)

// 还可以字符串直接是url
const myURL = new URL('https://example.org:8080/uer?abc=123#hash');
// const myURL = new URL('/aa','https://example.org:8080/uer?abc=123');
myURL.host = "www.baidu.com:334"
myURL.port = 22
console.log(myURL);
myURL.search = "abc=123&name=aa"
console.log(myURL.search); // ?abc=123&name=aa
console.log(myURL.href);

// 将对象转化成字符串


// '/one/two/four'  只能是一个参数
console.log(url.resolve('/one/two/three', 'four'));
// 'http://example.com/one'
console.log(url.resolve('http://example.com/', '/one'));
// 'http://example.com/two'
console.log(url.resolve('http://example.com/one/thress', '/two'));





